/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vale.sax.sintaxis;

import java.util.Stack;
import vale.util.StrOp;

/**
 *
 * @author juan
 */
class BalancedXml {

    private long roots;
    private boolean used;
    private Stack<String> stk;

    BalancedXml() {
        this.roots = 0;
        this.used = false;
        this.stk = new Stack<String>();
    }

    boolean isBalanced() {
        return this.used && this.stk.empty() && this.roots == 1;
    }

    void validate(String read, byte type) {
        this.used = true;
        String name = "";
        switch (type) {
            case ValidadorSintaxis.START_ELEMENT:
                name = StrOp.getNameOfTag(read, "<", ">");
                this.stk.push(name);
                break;
            case ValidadorSintaxis.END_ELEMENT:
                name = StrOp.getNameOfTag(read, "</", ">");
                if (!this.stk.empty() && this.stk.peek().equals(name)) {
                    this.stk.pop();
                } else {
                    this.stk.push(name);
                }
            case ValidadorSintaxis.AUTO_CLOSE_ELEMENT:
                if (this.stk.empty()) {
                    this.roots++;
                }
                break;
            default:
                break;
        }//switch
    }
}
